package com.caoxw.Android.CityBus;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.GestureDetector.OnGestureListener;
import android.view.View.OnTouchListener;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class ShowLinesActivity extends ActivityEx {
	private ListView m_objLinesList = null;
	private TextView m_objLineInfo = null;
	private String m_strStationA = null;
	private String m_strStationB = null;
	private LinesStruct m_objLBS = null;
	private Handler handler = null;
	private ProgressDialog m_dlgPro = null;
	private ArrayList<String> m_arrLines = null;

	
    private static final int FLING_MIN_DISTANCE = 100;      
    private static final int FLING_MIN_VELOCITY = 200;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.searchlines);
		m_objLineInfo = (TextView) this.findViewById(R.id.linesinfo);
		m_objLinesList = (ListView) this.findViewById(R.id.lineslist);
		Bundle bundle = getIntent().getExtras();
		m_strStationA = bundle.getString("StationA");
		m_strStationB = bundle.getString("StationB");
		m_objLineInfo.setText(m_strStationA + "→" + m_strStationB);
		m_objLBS = ((CityBusApp) getApplicationContext()).m_objBusScan
				.GetChangBusLines();
		handler = new Handler() {
			public void handleMessage(Message msg) {
				switch (msg.what) {
				case 0:
					m_objLinesList.setAdapter(fillListAdapt(m_arrLines));
					m_dlgPro.dismiss();
					break;
				}
			}
		};
		setListener();
		m_dlgPro = ProgressDialog.show(ShowLinesActivity.this,
				"请稍后...", "正在处理据...", true, false);
		// 新建线程
		new Thread() {
			@Override
			public void run() {
				// 需要花时间计算的方法
				// 向handler发消息
				fillData();
				handler.sendEmptyMessage(0);
			}
		}.start();
		//m_objLinesList.setLongClickable(true);
		//m_objLinesList.setOnTouchListener(this);
		

	}

	private void fillData() {
		m_arrLines = new ArrayList<String>();
		StringBuffer bf = new StringBuffer();
		for (int i = 0; i < m_objLBS.size(); i++) {
			bf = new StringBuffer();
			bf.append("方案" + String.valueOf(i) + " \t途经："
					+ String.valueOf(m_objLBS.get(i).TotalStations) + "站");
			for (int j = 0; j < m_objLBS.get(i).Buses.size(); j++) {
				bf
						.append(String
								.format(
										"\n\t%d:%s(%s%s行%d站)",
										j + 1,
										((CityBusApp) getApplicationContext()).m_objBusScan
												.GetStr(m_objLBS.get(i).Stations
														.get(j).intValue()),
										((CityBusApp) getApplicationContext()).m_objBusScan
												.GetStr(m_objLBS.get(i).Buses
														.get(j).intValue()),
										m_objLBS.get(i).Stations.get(j)
												.intValue() == 1 ? "上" : "下",
										m_objLBS.get(i).StationsCount.get(j)
												.intValue()));
			}
			m_arrLines.add(bf.toString());
		}
		

	}

	private void setListener() {

		m_objLinesList
				.setOnItemClickListener(new AdapterView.OnItemClickListener() {
					public void onItemClick(AdapterView<?> parent, View view,
							int position, long id) {
						m_objLinesList.requestFocusFromTouch();
						m_objLinesList.setSelection(position);
					}
				});

	}

	private SimpleAdapter fillListAdapt(ArrayList<String> listData) {
		ArrayList<HashMap<String, String>> listItem = new ArrayList<HashMap<String, String>>();
		for (int i = 0; i < listData.size(); i++) {
			HashMap<String, String> map = new HashMap<String, String>();
			// map.put("ItemImage", R.drawable.checked);//图像资源的ID
			map.put("ItemTitle", listData.get(i));
			// map.put("ItemText", "Finished in 1 Min 54 Secs, 70 Moves! ");
			listItem.add(map);
		}
		SimpleAdapter listItemAdapter = new SimpleAdapter(this, listItem,// 数据源
				R.layout.list_item_1,// ListItem的XML实现
				// 动态数组与ImageItem对应的子项
				new String[] { "ItemTitle" },
				// ImageItem的XML文件里面的一个ImageView,两个TextView ID
				new int[] { R.id.ItemTitle });
		return listItemAdapter;
	}

	public void onFlingLeft() {
		// TODO Auto-generated method stub
		  this.finish();
 
	}

}
